<?php
/**
 * 通用文档管理处理
 * article product ……
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\admin\model\Archive as Arc;

class Archive extends AdminBase
{
	protected $attachArray = [];
	public function _initialize()
	{

		parent::_initialize();

		$this->attachArray = [1=>'article',2=>'product'];

	}
	public function index($type)
	{
		$attachArray = [1=>'article',2=>'product'];

		if(!array_key_exists($type, $attachArray)) $this->error('分类不存在');

		return $t[$type];
	}

	public function lists($aid = 1)
	{
		$this->assign('pageConfig',json_encode([
			'base_uri'=> '/' . MODULE_NAME . '/' . $this->attachArray[$aid],
			'aid'	=>	$aid,
			'attach' => $this->attachArray,
			]));
		return $this->fetch();
	}

	public function getData($aid = 1)
	{
		//手动构建
		$attachArray = $this->attachArray;
		if(!array_key_exists($aid, $attachArray)) $this->error('Attach不存在');
		//接受部分参数
		$pageIndex = $this->request->param('pageIndex');
		$pageSize = $this->request->param('pageSize');
		//条件限制
		$condition = [
			'attach_type'	=>	$aid,
			'status'		=>	0,
		];
		$field = 'id,title,description,attach_id,create_time,update_time,category_id,status';

		//Archive数据查询
		$count = Arc::has( $attachArray[$aid] )->where($condition)->count('id');
		$archive = Arc::has( $attachArray[$aid] )->field($field)->where($condition)->limit(($pageIndex-1)*$pageSize,$pageSize)->order('create_time desc')->select();

		//构造返回数据
		$json = [
			'rel'	=>	true,
			'msg'	=>	'获取成功',
			'list'	=>	$archive,
			'count'	=>	$count,
		];
		return json($json)
			->code(201)
			->header([
				'Cache-control' => 'no-cache,must-revalidate',
				'Access-Control-Allow-Origin' =>	'*',

				]);
	}
}